#!/usr/bin/ruby

require 'test/unit'
require 'lib/rire'

class TestTail < Test::Unit::TestCase
  def setup
    @dsl = Rire::DSL.new($rire_home + '/plugin')
    @dsl.instance_eval <<-EOD
      def execute(src)
        instance_eval(src, 'unit.dsl', 1)
      end
    EOD
  end

  def test_no_num
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :link => 'http://www.example.com/1' },
          { :link => 'http://www.example.com/2' },
          { :link => 'http://www.example.com/3' },
          { :link => 'http://www.example.com/4' },
          { :link => 'http://www.example.com/5' },
        ]

        filter.tail
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('http://www.example.com/1', ctx.entries[0].link)
    assert_equal('http://www.example.com/2', ctx.entries[1].link)
    assert_equal('http://www.example.com/3', ctx.entries[2].link)
    assert_equal('http://www.example.com/4', ctx.entries[3].link)
    assert_equal('http://www.example.com/5', ctx.entries[4].link)
    assert_equal(5, ctx.entries.length)
  end

  def test_num
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :link => 'http://www.example.com/1' },
          { :link => 'http://www.example.com/2' },
          { :link => 'http://www.example.com/3' },
          { :link => 'http://www.example.com/4' },
          { :link => 'http://www.example.com/5' },
        ]

        filter.tail :config => { :num => 3 }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('http://www.example.com/3', ctx.entries[0].link)
    assert_equal('http://www.example.com/4', ctx.entries[1].link)
    assert_equal('http://www.example.com/5', ctx.entries[2].link)
    assert_equal(3, ctx.entries.length)
  end

  def teardown
  end
end

